package com.jzo2o.orders.base.handler;

import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.base.model.dto.OrderSnapshotDTO;
import com.jzo2o.orders.base.service.IOrdersCommonService;
import com.jzo2o.statemachine.core.StatusChangeEvent;
import com.jzo2o.statemachine.core.StatusChangeHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 订单派单取消处理器
 */
@Slf4j
@Component("order_close_dispatching_order")
public class OrderCloseDispatchingHandler implements StatusChangeHandler<OrderSnapshotDTO> {
    @Resource
    private IOrdersCommonService ordersService;

    /**
     * 订单支付处理逻辑
     *
     * @param bizId   业务id
     * @param bizSnapshot 快照
     */
    @Override
    public void handler(String bizId, StatusChangeEvent statusChangeEventEnum, OrderSnapshotDTO bizSnapshot) {
        log.info("订单派单取消事件处理逻辑开始，订单号：{}", bizId);
        boolean update = ordersService.lambdaUpdate()
                .set(Orders::getRefundId, bizSnapshot.getThirdRefundOrderId())
                .set(Orders::getRefundNo, bizSnapshot.getRefundNo())
                .set(Orders::getRefundStatus, bizSnapshot.getRefundStatus())
                .set(Orders::getOrdersStatus, OrderStatusEnum.CLOSED.getStatus())
                .eq(Orders::getId, bizSnapshot.getId())
                .update();
        if(!update){
            throw new DBException("退款，修改订单失败");
        }
        log.info("订单派单取消事件处理逻辑结束，订单号：{}", bizId);
    }


}